import * as AppConstant from '../constant/AppConstant'

const FormatUtil = {
  // -----格式化手机号(加空格)-----
  formatPhone: (phone) => {
    return phone.replace(/\B(?=(\d{4})+(?!\d))/g, ' ')
  },
  // -----格式化银行卡号-----
  formatBankCardNum: (str) => {
    return str.replace(/(\d{4})/g, '$1 ')
  },

  // -----去空格-----
  formatSpace: (string) => {
    return string.replace(/\s+/g, '')
  },
  // -----金融数字加逗号-----
  formatMoneyNum: (string, isShowDecimal) => {
    const str = string + '' // 强转字符串
    // 当数字是整数
    if (str.indexOf('.') === -1) {
      if (isShowDecimal === true) {
        return str.replace(/\B(?=(\d{3})+\b)/g, ',') + '.00'
      } else {
        return str.replace(/\B(?=(\d{3})+\b)/g, ',')
      }
    } else { // 当数字带有小数
      return str.replace(/(?=\B(?:\d{3})+\b)(\d{3}(?:\.\d+$)?)/g, ',$1')
    }
  },

  // -----去掉逗号-----
  formatComma: (str) => {
    return str.replace(/,/g, '')
  },

  // -----key等于null的时候赋值为空字符串-----
  validateDictionary: (dict) => {
    Object.keys(dict).forEach((key) => {
      if (dict[key] == null) {
        dict[key] = ''
      }
    })
    return dict
  },

  // -----string等于null的时候赋值为空字符串-----
  validateString: (string) => {
    if (string == null) {
      string = ''
    }
    return string
  },

  extractChinese: (str) => {
    if (str !== null && str !== '') {
      const reg = /[\u4e00-\u9fa5]+/g
      return str.match(reg)
    } else return ''
  },

  cipherString: (str, start, end) => {
    if (str && end > start && str.length >= end) {
      let newStr = str.substring(0, start)
      const cipherStr = str.substring(start, end)
      for (let i = 0; i < cipherStr.length; i++) {
        newStr += '*'
      }
      if (end < str.length) {
        newStr += str.substring(end, str.length)
      }
      return newStr
    }
    return str
  },

  cipherName: (str) => {
    if (str && str.length > 0) {
      return str.substring(0, 1) + '**'
    }
    return str
  },

  cipherMobile: (str) => {
    if (str && str.length > 4) {
      return FormatUtil.cipherString(str, 3, str.length - 4)
    }
  },
  // decodeBase64Content: (str) => {
  //     let Buffer = require('buffer').Buffer
  //     let b = new Buffer(str, 'base64')
  //     let s = b.toString()
  //     return s
  // },

  // 过滤空字符串
  normalizeValue: (value) => {
    if (typeof value === 'undefined' || value === null) {
      return ''
    }
    return value + ''
  },

  // 获取一个银行卡的所有信息
  getBankCardInfo: (dataArray) => {
    const newArr = []
    dataArray.forEach((e) => {
      const bankCode = e.bankCode
      AppConstant.BANKCARD_ARRAY.forEach((item, index) => {
        if (item.code === bankCode) {
          newArr.push(Object.assign(e, item))
        }
      })
    })
    return newArr
  },

  // 根据bank值获取一个银行卡的所有信息
  getBankCardDict: (bank) => {
    const newArr = AppConstant.BANKCARD_ARRAY.filter((e) => {
      return e.bank === bank
    })
    if (newArr.length) {
      return newArr[0]
    }
    return null
  },

  concatDictionary: (dict1, dict2) => {
    const newDict = dict1
    Object.keys(dict2).forEach((key) => {
      if (dict2[key] === null) {
        newDict[key] = ''
      } else {
        newDict[key] = dict2[key]
      }
    })
    return newDict
  },

  formatDateString: (time, format = 'YY-MM-DD hh:mm:ss') => {
    const date = new Date(time)

    const year = date.getFullYear()
    const month = date.getMonth() + 1 // 月份是从0开始的
    const day = date.getDate()
    const hour = date.getHours()
    const min = date.getMinutes()
    const sec = date.getSeconds()
    const preArr = Array.apply(null, Array(10)).map(function (elem, index) {
      return '0' + index
    }) // 开个长度为10的数组 格式为 00 01 02 03

    const newTime = format
      .replace(/YY/g, year)
      .replace(/MM/g, preArr[month] || month)
      .replace(/DD/g, preArr[day] || day)
      .replace(/hh/g, preArr[hour] || hour)
      .replace(/mm/g, preArr[min] || min)
      .replace(/ss/g, preArr[sec] || sec)

    return newTime
  },
}

export default FormatUtil
